package classes.view.component
{
	
	import classes.controller.DAOSendFlash;
	import classes.controller.ENumber;
	
	import com.greensock.TweenLite;
	import com.greensock.easing.Quart;
	
	import fl.controls.UIScrollBar;
	
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	public class QuizContainer extends MovieClip
	{
		private var tornPage : MovieClip;
		private var tornMC : MovieClip;
		private var scrollbg :MovieClip;
		private var whiteNote : MovieClip;
		private var scrollbar :UIScrollBar;
		private var delBtn : MovieClip; 
		private var delAlert : MovieClip;
		private var importantBtn : MovieClip;
		
		private var currQuiz  :QuizNode;
		
		
		public function QuizContainer()
		{
			super();
			this.gotoAndStop("small_box");
			init();
			
		}
		
		private function initLayout():void
		{
			tornPage = this.getChildByName("torn_page") 				as MovieClip;
			tornMC = tornPage.getChildAt(0) 							as MovieClip;
			scrollbg = tornMC.getChildByName("scroll_background") 		as MovieClip;
			scrollbar = tornMC.getChildByName("title_scrollbar") 		as UIScrollBar;
			whiteNote = tornMC.getChildByName("white_note") 			as MovieClip;
			delBtn = this.getChildByName("del_btn") 					as MovieClip;
			delAlert = this.getChildByName("del_alert")					as MovieClip;
			importantBtn = this.getChildByName("important") 			as MovieClip;
			
		}
		
		private function init():void
		{
			//delAlert.
		}
		
		public function showQuizContent(_node : QuizNode, _totalNum : int, _index:int) : void
		{
			
			if(_node == null || _node.index == 0) // 첫번째 박스 혹은 마지막 박스는 화면에서 가리기
			{
				this.visible = false;
				return;
			}
			
			
			selectQuizBox(_node.isBig); //큰박스인지 작은 박스인지 결정 
			currQuiz = _node;
			
			isImportant = _node.isImportant; // 처음엔 항상 중요 문제로 지정(true)
			
			(this.getChildByName("curr_text") as TextField).text = _node.index.toString(10);
			(this.getChildByName("total_text") as TextField).text = _totalNum.toString(10); ;
			hideScrollBar();
			
			if(_index == 1 || _index == 2){
				(tornMC.getChildByName("catagory_text") as TextField).text = _node.catagory;
				(tornMC.getChildByName("title_text") as TextField).text = _node.title;
				if(_node.mode != "ox") (tornMC.getChildByName("title_text") as TextField).appendText("\n\n" + _node.choiceString); 
				
				
				
				if(_node.isBig == true && currentFrameLabel == "big_box")
				{
					var titleText : TextField = tornMC.getChildByName("title_text") as TextField;
					if(titleText.numLines > titleText.bottomScrollV) showScrollBar();
				}
			}
			this.visible = true;
		}
		
		private function showScrollBar():void
		{	
			var scrollbar :UIScrollBar = tornMC.getChildByName("title_scrollbar") as UIScrollBar;
			scrollbar.scrollTarget = tornMC.getChildByName("title_text");
			whiteNote.mouseEnabled = false;  
			
			scrollbar.setStyle("scrollBarWidth",15);
			scrollbar.setStyle("scrollArrowHeight",15);
			
			scrollbg.visible = true; 
			scrollbar.visible = true; 
		}
		
		private function hideScrollBar():void
		{
			scrollbg.visible = false; 
			scrollbar.visible = false; 
		}
		
		private function importantBtnHandler(event : MouseEvent): void
		{
			
			currQuiz.isImportant = !currQuiz.isImportant; 
			
			if(currQuiz.isImportant)
				dispatchEvent(new ENumber(2,"odabClick")); 
			else 
				dispatchEvent(new ENumber(1,"odabClick"));
			
			this.isImportant = currQuiz.isImportant; 
			
		}
		
		private function delAlertHandler(event : MouseEvent) :void
		{	
			
			if(event.target == delAlert.ok_btn )
			{
				dispatchEvent(new ENumber(0,"odabClick")); 
				currQuiz.isQuizSaved = false; 
				delAlert.visible = false; 
				delAlert.removeEventListener(MouseEvent.CLICK,delAlertHandler);
			}
			else if(event.target == delAlert.no_btn)
			{
				currQuiz.isQuizSaved = true; 
				delAlert.visible = false;
				delAlert.removeEventListener(MouseEvent.CLICK,delAlertHandler);
			}
			
		}
		
		private function delBtnHandler(event : MouseEvent) :void
		{
			delAlert.visible = true; 
			
		}
		
		
		private function selectQuizBox(_isBig : Boolean):void
		{
			if(_isBig == true)
			{
				this.gotoAndStop("big_box"); 
				
			}
			else{
				this.gotoAndStop("small_box");
			}
			
			
			initLayout();
			delAlert.visible = false; 
			showYellowNote();
			if(!importantBtn.hasEventListener(MouseEvent.CLICK)) importantBtn.addEventListener(MouseEvent.CLICK, importantBtnHandler);
			if(!delBtn.hasEventListener(MouseEvent.CLICK)) delBtn.addEventListener(MouseEvent.CLICK, delBtnHandler);
			if(!delAlert.ok_btn.hasEventListener(MouseEvent.CLICK)) delAlert.ok_btn.addEventListener(MouseEvent.CLICK, delAlertHandler);
			if(!delAlert.no_btn.hasEventListener(MouseEvent.CLICK)) delAlert.no_btn.addEventListener(MouseEvent.CLICK, delAlertHandler); 
			tornPage.gotoAndStop(1);
		}
		
		
		public function playToYellowNote():void
		{
			tornPage = this.getChildByName("torn_page") 				as MovieClip;
			tornMC = tornPage.getChildAt(0) 							as MovieClip;
			whiteNote = tornMC.getChildByName("white_note") 			as MovieClip;
			
			TweenLite.to(whiteNote, 0.5 ,{alpha:0 ,  ease:Quart.easeOut});
			
		}
		
		public function playToWhiteNote():void
		{
			tornPage = this.getChildByName("torn_page") 				as MovieClip;
			tornMC = tornPage.getChildAt(0) 							as MovieClip;
			whiteNote = tornMC.getChildByName("white_note") 			as MovieClip;
			TweenLite.to(whiteNote, 0.4,{alpha:1,  ease:Quart.easeIn});	
		}
		
		public function showYellowNote():void
		{
			tornPage = this.getChildByName("torn_page") 				as MovieClip;
			tornMC = tornPage.getChildAt(0) 							as MovieClip;
			whiteNote = tornMC.getChildByName("white_note") 			as MovieClip;
			whiteNote.alpha = 0;
			
		}
		
		public function showWhiteNote():void
		{
			tornPage = this.getChildByName("torn_page") 				as MovieClip;
			tornMC = tornPage.getChildAt(0) 							as MovieClip;
			whiteNote = tornMC.getChildByName("white_note") 			as MovieClip;
			whiteNote.alpha = 1;
			
		}
		
		
		public function showODab(_node:QuizNode):void
		{
			
			(this.getChildByName("percentage") as TextField).text = _node.percentage + "%";
			(this.getChildByName("source") as TextField).text = _node.source;
			(this.getChildByName("chapter") as TextField).text = _node.chapter;
			(this.getChildByName("correct_answer") as TextField).text = _node.answerString;
			(this.getChildByName("explaination") as TextField).text = _node.explaination;
			
			tornPage.play();
		}
		
		private function set isImportant ($bool: Boolean) :void
		{	
			if($bool == true) importantBtn.gotoAndStop(2); 
			else importantBtn.gotoAndStop(1);
		}
		
		
		//게임 종료 후 이벤트 리스너 등록제거
		public function removeChildEventListenter():void
		{
			this.gotoAndStop("small_box");
			
			delBtn = this.getChildByName("del_btn") as MovieClip;
			delAlert = this.getChildByName("del_alert") as MovieClip;
			importantBtn = this.getChildByName("important") as MovieClip;
			
			if(importantBtn.hasEventListener(MouseEvent.CLICK)) importantBtn.removeEventListener(MouseEvent.CLICK, importantBtnHandler);
			if(delBtn.hasEventListener(MouseEvent.CLICK)) delBtn.removeEventListener(MouseEvent.CLICK, delBtnHandler);
			if(delAlert.hasEventListener(MouseEvent.CLICK)) delAlert.removeEventListener(MouseEvent.CLICK, delAlertHandler); 
			
			this.gotoAndStop("big_box");
			
			delBtn = this.getChildByName("del_btn") as MovieClip;
			delAlert = this.getChildByName("del_alert") as MovieClip;
			importantBtn = this.getChildByName("important") as MovieClip;
			
			if(importantBtn.hasEventListener(MouseEvent.CLICK)) importantBtn.removeEventListener(MouseEvent.CLICK, importantBtnHandler);
			if(delBtn.hasEventListener(MouseEvent.CLICK)) delBtn.removeEventListener(MouseEvent.CLICK, delBtnHandler);
			if(delAlert.ok_btn.hasEventListener(MouseEvent.CLICK)) delAlert.ok_btn.removeEventListener(MouseEvent.CLICK, delAlertHandler);
			if(delAlert.no_btn.hasEventListener(MouseEvent.CLICK)) delAlert.no_btn.removeEventListener(MouseEvent.CLICK, delAlertHandler); 
			
		}
		
		
	}
}